package com.atnjupt.service.impl;

import com.atnjupt.bean.Book;
import com.atnjupt.bean.Page;
import com.atnjupt.dao.impl.BookDaoImpl;
import com.atnjupt.service.BookService;

import java.util.List;

/**
 * @author Wizarder
 * @create 2021-06-04 20:14
 */
public class BookServiceImpl implements BookService {
    private BookDaoImpl bookService = new BookDaoImpl();
    @Override
    public void addBook(Book book) {
        bookService.addBook(book);
    }

    @Override
    public void deleteBookById(int id) {
        bookService.deletBookById(id);
    }


    @Override
    public void updateBook(Book book) {
        bookService.updateBook(book);
    }

    @Override
    public Book queryBookById(int id) {
        return bookService.queryBookById(id);
    }

    @Override
    public List<Book> queryBooks() {
        return bookService.queryBooks();
    }

    @Override
    public Page<Book> page(int pageNo, int pageSize) {
        Page<Book> page = new Page();

        //设置每页显示条目数
        page.setPageSize(pageSize);
        //设置总条目数
        int queryPageTotalCount = bookService.queryPageTotalCount();
        page.setPageTotalCount(queryPageTotalCount);

        //设置总页码
        int pageTotal = queryPageTotalCount / pageSize;
        if ((queryPageTotalCount % pageSize)>0){
            pageTotal++;
        }
        page.setPageTotal(pageTotal);
        //设置页码 里面要进行页码校验 所以得放在pageTotal后面
        page.setPageNo(pageNo);
        //设置每页显示的内容数据
        List<Book> books = bookService.queryForPageItems((page.getPageNo() - 1) * pageSize, pageSize);
        page.setItems(books);
        return page;
    }

    @Override
    public int pageTotalCount() {
        return bookService.queryPageTotalCount();
    }

    @Override
    public Page<Book> pageByPrice(int pageNo, int pageSize, int min, int max) {
        Page<Book> page = new Page();

        //设置每页显示条目数
        page.setPageSize(pageSize);
        //根据价格设置总条目数
        int queryPageTotalCount = bookService.queryPageTotalCountByPrice(min,max);
        page.setPageTotalCount(queryPageTotalCount);

        //设置总页码
        int pageTotal = queryPageTotalCount / pageSize;
        if ((queryPageTotalCount % pageSize)>0){
            pageTotal++;
        }
        page.setPageTotal(pageTotal);
        //设置页码 里面要进行页码校验 所以得放在pageTotal后面
        page.setPageNo(pageNo);
        //设置每页显示的内容数据
        List<Book> books = bookService.queryForPageItemsByPrince((page.getPageNo() - 1) * pageSize, pageSize,min,max);
        page.setItems(books);
        return page;
    }
}
